package all.com.jinyongqunxiax.modules;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.randy.fundation.JxFunc;
import com.randy.fundation.Logger;

import all.com.jinyongqunxiax.R;
import all.com.jinyongqunxiax.component.JxForm;
import all.com.jinyongqunxiax.component.JxFormHeader;
import all.com.jinyongqunxiax.component.JxFormItemText;
import all.com.jinyongqunxiax.databinding.StoryDetailActivityBinding;
import all.com.jinyongqunxiax.model.mod.StoryManager;
import all.com.jinyongqunxiax.model.mod.manager.Mod;
import all.com.jinyongqunxiax.model.mod.manager.ModManager;
import all.com.jinyongqunxiax.view.JxHud;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;
import jy.game.JyCondition;
import jy.game.components.story.JyStory;
import jy.game.components.story.JyStoryAction;
import jy.game.components.story.JyStoryResult;

public class StoryDetailActivity extends AppCompatActivity {
    private Options                    options;
    private StoryDetailActivityBinding binding;
    JxForm form = new JxForm();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        options = new Options(getIntent());

        setTitle(options.item);

        Mod          mod     = ModManager.shared().get(options.mod);
        StoryManager manager = mod.storyManager;
        JyStory      model   = manager.map.get(options.item);

        binding = DataBindingUtil.setContentView(this, R.layout.story_detail_activity);

        RecyclerView recyclerView = binding.recyclerView;
        form.connect(this, recyclerView);

        if (model != null && model.actions != null) {
            for (Object baseAction : model.actions) {
                JxFunc.as(baseAction, JyStoryAction.class).ifPresent(action -> {
                    switch (action.type) {
                        case "SELECT": {
                            String[] parts = action.value.split("#");
                            if (parts.length > 2) {
                                String title = parts[0].trim() + ":" + parts[1].trim();
                                form.add(new JxFormHeader(0, title, null));
                                for (int i = 2; i < parts.length; i++) {
                                    String value = parts[i].trim();
                                    form.add(new JxFormItemText(0, JxForm.TextField.Builder
                                        .build()
                                        .label("" + (i - 2))
                                        .value(value).valueAction(JxForm.FieldAction.SINGLE_PICK)
                                        .hint("").value(), null));
                                }
                            }
                        }
                        break;
                        case "DIALOG": {
                            JxFunc.of(action.value.split("#"))
                                .map(o -> TextUtils.isEmpty(o.trim()) ? null : o)
                                .zipAsList().ifPresent(parts -> {
                                if (parts.size() > 1) {
                                    form.add(new JxFormItemText(0, JxForm.TextField.Builder
                                        .build()
                                        .label(parts.get(0))
                                        .value(parts.get(1)).valueAction(JxForm.FieldAction.MULTI_LINE)
                                        .hint("").value(), null));
                                } else {
                                    Logger.w("Invalid Dialog:%s", action.value);
                                }
                            });
                        }
                        break;
                        case "BATTLE": {
                            form.add(new JxFormHeader(0, Mod.typeName(action.type) + " " + action.value, null));
                        }
                        break;

                        default:
                            form.add(new JxFormItemText(0, JxForm.TextField.Builder.build().label(Mod.typeName(action.type)).value(action.value).hint("").value(), null));
                    }
                });

                JxFunc.as(baseAction, JyStoryResult.class).ifPresent(result -> JxFunc.of(result.conditions).zipAsList().ifPresent(conditions -> {
                    if (conditions.size() == 0) {
                        form.add(new JxFormItemText(0, JxForm.TextField.Builder.build().label(Mod.typeName(result.type)).value(result.ret + " " + result.value).hint("").value(), null));
                        return;
                    }
                    form.add(new JxFormHeader(0, Mod.typeName(result.type) + ":" + result.ret + " " + result.value, null));
                    for (JyCondition condition : conditions) {
                        form.add(new JxFormItemText(0, JxForm.TextField.Builder.build().label(Mod.typeName(condition.type)).value(condition.value).hint("").value(), null));
                    }
                }));
            }
        }
        form.refresh();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.plus_button_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_add_button:
                JxHud.info("add menu clicked");
                break;
            default:
                Logger.w("not handle menu %d", item.getItemId());
                break;
        }
        return true;
    }

    public static class Options {
        String mod;
        String item;

        Options(Intent intent) {
            this.mod = intent.getStringExtra("mod");
            this.item = intent.getStringExtra("item");
        }

        static Intent intent(Context context, String mod, String item) {
            Intent intent = new Intent(context, StoryDetailActivity.class);
            intent.putExtra("mod", mod);
            intent.putExtra("item", item);
            return intent;
        }
    }
}
